//
//  StoreManageController.m
//  ios-supplier 店铺维护Controller
//
//  Created by feynmanyuan on 15/4/17.
//  Copyright (c) 2015年 feynmanyuan. All rights reserved.
//

#import "StoreManageController.h"
#import "JFBStoreInfoDS.h"
#import "JFBPhoneCallHelper.h"
#import "StoreManagerApi.h"

#import "ApiGetMerchantInfo.h"

@interface StoreManageController ()<UITableViewDataSource, UITableViewDelegate, UIAlertViewDelegate>
@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (weak, nonatomic) IBOutlet UILabel *storeNameLabel;//店铺名字
@property (weak, nonatomic) IBOutlet UILabel *storeIDLabel;//店铺ID
//@property (weak, nonatomic) IBOutlet UILabel *workTimeLabel;
@property (weak, nonatomic) IBOutlet UILabel *callcenterLabel;//店主姓名
@property (weak, nonatomic) IBOutlet UILabel *minLabel;
@property (weak, nonatomic) IBOutlet UILabel *distributionLabel;
//@property (weak, nonatomic) IBOutlet UILabel *timeRangeLabel;
@property (weak, nonatomic) IBOutlet UILabel *masterNameLabel;//店主电话
@property (weak, nonatomic) IBOutlet UILabel *storeAddressLabel;//店铺地址
@property (weak, nonatomic) IBOutlet UISwitch *isStoreOpenSW;//是否营业
@property (weak, nonatomic) IBOutlet UILabel *addressNameLabel;//地址
@property (weak, nonatomic) IBOutlet UIImageView *idImage;

#define screen_Width [UIScreen mainScreen].bounds.size.width
#define screen_Height [UIScreen mainScreen].bounds.size.height

@property (weak, nonatomic) IBOutlet UIScrollView *scrollView;

@property (nonatomic, strong) JFBStoreInfoDS *storeInfo;

@property (nonatomic, strong) JFBPhoneCallHelper *helper;
- (IBAction)swStoreOptAction:(id)sender;
- (IBAction)observeSwStoreChange:(id)sender;

@end

@implementation StoreManageController
-(void)viewWillAppear:(BOOL)animated{
    self.navigationController.navigationBar.hidden = YES;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    [self loadData];
    self.helper = [[JFBPhoneCallHelper alloc] initWithMobile:@"400-056-6565" color:nil];
    //把店铺ID图片切成圆角
    self.idImage.layer.masksToBounds = YES;
    self.idImage.layer.cornerRadius = 4.0f;

    self.scrollView.contentSize = CGSizeMake(self.view.bounds.size.width, CGRectGetMaxY(self.tableView.frame));
    
    [self.dianPu setImage:[UIImage imageNamed:@"开"] forState:UIControlStateNormal];
    [self.dianPu setImage:[UIImage imageNamed:@"关闭"] forState:UIControlStateSelected];
    
    [self refreshInfoUI];
}
-(UIStatusBarStyle)preferredStatusBarStyle
{
    return UIStatusBarStyleLightContent;
}
- (void) refreshInfoUI{
    //给我的店铺的五个参数赋值
    self.storeNameLabel.text    = [UserInfo currentUser].contactName;
    self.storeIDLabel.text      = [UserInfo currentUser].merchantID;
    self.callcenterLabel.text   = [UserInfo currentUser].shopkeeper;
    self.masterNameLabel.text   = [UserInfo currentUser].contactMobile;
    self.storeAddressLabel.text = [UserInfo currentUser].contactAddr;
}
#pragma mark -请求数据
-(void)loadData{
    [[[ApiGetMerchantInfo alloc]init]startWithCompletionSuccess:^(id JSON, YTKBaseRequest *request) {
        if ([JSON[@"code"] integerValue] == 0) {
            [[UserInfo currentUser]setupWithDictionary:JSON[@"data"]];
            [self refreshInfoUI];
        }else if ([JSON[@"code"] integerValue] == 99){
            UIViewController* viewController;
            UIStoryboard *mainStoryBoard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
            viewController = [mainStoryBoard instantiateViewControllerWithIdentifier:@"Login"];
        }
    } failure:^(YTKBaseRequest *request) {
        
    }];

//    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
//    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
//    NSDictionary *dic = @{@"ut":[[NSUserDefaults standardUserDefaults] objectForKey:@"ut"]};
//    [manager GET:@"http://118.186.246.206:8612/back-user/queryShopByMerchant.do" parameters:dic success:^(AFHTTPRequestOperation *operation, id responseObject) {
//        id obj = [NSJSONSerialization JSONObjectWithData:responseObject options:kNilOptions error:nil];
//        NSObject * error = [obj valueForKey:@"code"];
//        NSDictionary *data = [obj valueForKey:@"data"];
//        if ([error isEqual:@"0"]) {
//            //给我的店铺的五个参数赋值
//            self.storeNameLabel.text = [NSString stringWithFormat:@"%@",[data valueForKey:@"name"]];
//            self.storeIDLabel.text = [NSString stringWithFormat:@"%@",[data valueForKey:@"merchantId"]];
//            self.callcenterLabel.text = [NSString stringWithFormat:@"%@",[data valueForKey:@"shopkeeper"]];
//            self.masterNameLabel.text = [NSString stringWithFormat:@"%@",[data valueForKey:@"contactMobile"]];
//            self.storeAddressLabel.text = [NSString stringWithFormat:@"%@",[data valueForKey:@"contactAddr"]];
//            self.storeStatus = [data valueForKey:@"status"];
//        }
//    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
//        
//    }];
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 3;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return section?1:2;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell * cell;
    if( indexPath.section == 0 ) {
        if (indexPath.row == 0) {
            static NSString *cellIdentifier = @"orderGoods";
            cell=[tableView dequeueReusableCellWithIdentifier:cellIdentifier];
            cell.textLabel.text = @"订购商品";
        }else if (indexPath.row ==1){
            static NSString *cellIdentifier = @"ManIdentifier";
            cell=[tableView dequeueReusableCellWithIdentifier:cellIdentifier];
            cell.textLabel.text = @"配送员管理";
        }
    } else if( indexPath.section == 1 ) {
        static NSString *cellIdentifier = @"SettingIdentifier";
        cell=[tableView dequeueReusableCellWithIdentifier:cellIdentifier];
        cell.textLabel.text = @"系统设置";
    } else if( indexPath.section == 2 ){
        static NSString *cellIdentifier = @"CallCenterIdentifier";
        cell=[tableView dequeueReusableCellWithIdentifier:cellIdentifier];
        cell.textLabel.text = @"客服电话";
        cell.detailTextLabel.text = @"400-056-6565";
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;

    return cell;
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 1;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 44;
}
-(void)viewDidLayoutSubviews
{
    if ([self.tableView respondsToSelector:@selector(setSeparatorInset:)]) {
        [self.tableView setSeparatorInset:UIEdgeInsetsMake(0,0,0,0)];
    }
    
    if ([self.tableView respondsToSelector:@selector(setLayoutMargins:)]) {
        [self.tableView setLayoutMargins:UIEdgeInsetsMake(0,0,0,0)];
    }
}

-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if ([cell respondsToSelector:@selector(setSeparatorInset:)]) {
        [cell setSeparatorInset:UIEdgeInsetsZero];
    }
    
    if ([cell respondsToSelector:@selector(setLayoutMargins:)]) {
        [cell setLayoutMargins:UIEdgeInsetsZero];
    }
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0) {
        if (indexPath.row == 0) {
            
        }else if( indexPath.row == 1) {
                [self performSegueWithIdentifier:@"runLegManagerIdentifiler" sender:self.storeInfo];
            }
    }
     else if(indexPath.section == 1) {
        [self performSegueWithIdentifier:@"systemConfigIdentifiler" sender:self];
    } else if(indexPath.section == 2){
        [self.helper call];
    }
}

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    ((UIViewController*)segue.destinationViewController).hidesBottomBarWhenPushed = true;
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
    if( buttonIndex == 1 ) {
        [SVProgressHUD showWithMaskType:SVProgressHUDMaskTypeBlack];
        [JFBStoreInfoDS updateOperating:^(BOOL isSuccess, NSString *message, JFBStoreInfoDS * ds, NSError *error) {
            [SVProgressHUD dismiss];
            if( isSuccess ) {
                self.storeInfo = ds;
                [JFBGlobalSetting getSetting].isStoreStateChange = TRUE;
                [JFBUIAnimation alertTips:message];
            }
            
        } currentStatus:self.storeInfo.isStoreOpen];
    }
}

- (IBAction)swStoreOptAction:(id)sender {
    [JFBUIAnimation alertTips:@"暂不允许操作进入歇业状态！"];
//    NSString * tips = [NSString stringWithFormat:@"确定设置店铺%@?", self.storeInfo.isStoreOpen == 0 ? @"营业" : @"歇业"];
//    
//    UIAlertView *alert = [[UIAlertView alloc]
//                          initWithTitle:@"提示"
//                          message:tips
//                          delegate:self
//                          cancelButtonTitle:@"取消"
//                          otherButtonTitles:@"确定",nil];
//    [alert show];
//    [self.isStoreOpenSW setUserInteractionEnabled: YES]; // 只能设置为开业，不能歇业；
}

- (IBAction)observeSwStoreChange:(id)sender { // 当 isStoreOpenSW 客户可以设置开、关时此方法无用
    if (self.isStoreOpenSW.on == YES)
    {
        [JFBUIAnimation alertTips: @"暂不允许操作进入歇业状态！"];
    }
    else
    {
        [self.isStoreOpenSW setOn: YES animated: YES];
        [self swStoreOptAction:nil];
    }
}
-(void)viewWillDisappear:(BOOL)animated{
    self.navigationController.navigationBar.hidden = NO;
}
@end
